package translinkApp.DialogBoxes.SavedLocations;

import translinkApp.Data.ResolvedLocation.ResolvedLocationList;
import translinkApp.Utilities.DataResource;
import translinkApp.Utilities.Utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class SavedLocationsDialog {
	
	public static void showDialog(Context c, final OnReturnLocationListener listener) {
		showDialog(c, listener, "Pick a location");

	}

	public static void showDialog(Context c, final OnReturnLocationListener listener, String title) {
		final ResolvedLocationList locations = DataResource.savedLocations.getData();
		locations.sort();
		if (locations.size() == 0) {
			Utils.showAlertDialog(c,
					"There are no saved searches. Please add these through the settings menu.");
			return;
		}

		CharSequence[] items = new CharSequence[locations.size()];

		for (int i = 0; i < locations.size(); i++) {
			items[i] = locations.get(i).getDisplayAddress();
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(c);
		builder.setTitle(title);

		builder.setItems(items, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int item) {

				listener.onReturnLocation(locations.get(item));

			}
		});

		AlertDialog create = builder.create();
		create.show();

	}

}
